﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noris.Schedule.UI.Components;

namespace Noris.Schedule.Schedule.Forms
{
    public partial class _TestColumns : Form
    {
        public _TestColumns()
        {
            InitializeComponent();
            _InitColumns();
        }


		private void _TestColumns_Load(object sender, EventArgs e)
		{
			this.CancelButton = this.CloseBtn;
		}
		private void Close_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		private void _InitColumns()
        {
			_PrepareColumns(this.Columns1);
			// _PrepareEvents(this.Columns1);
			_PrepareColumns(this.Columns2);

            DColumnsSynchronizedItems items =
                DColumnsSynchronizedItems.ColumnHeaderHeightChanging |
                DColumnsSynchronizedItems.ColumnHeaderHeightChanged |
                DColumnsSynchronizedItems.ColumnWidthChanging |
                DColumnsSynchronizedItems.ColumnWidthChanged |
                DColumnsSynchronizedItems.ColumnOrderChanged |
                DColumnsSynchronizedItems.ColumnSortChanged |
                DColumnsSynchronizedItems.TimeAxisRangeChanged;

            DColumnsSynchronizer sync = new DColumnsSynchronizer(items, false);
			_PrepareEvents(sync);
			this.Columns1.Synchronizer = sync;
			this.Columns2.Synchronizer = sync;
        }

		private void _PrepareColumns(DColumns columns)
		{
			columns.BorderStyle = BorderStyle.Fixed3D;
            columns.ColumnHeaderMoveMode = DColumnsHeaderMoveMode.MoveInParent;
            columns.ColumnHeaderTopUpperLimit = 2;
            columns.ColumnHeaderTopDownLimit = -60;


			var c1 = columns.ColumnAdd("Reference", "refer", 160);
			c1.RowFilterVisible = true;
			c1.IsSortEnabled = true;

			var c2 = columns.ColumnAdd("Název", "name", 320);
			c2.RowFilterVisible = true;
			c2.IsSortEnabled = true;

			var c3 = columns.ColumnAdd("", "timeaxis", 320);
			c3.HeaderMode = UI.Components.DColumnHeaderMode.TimeAxis;
			c3.RowFilterVisible = true;
			c3.IsSortEnabled = false;
			c3.ResizeWidthToFillParent = true;
		}
		private void _PrepareEvents(DColumnsSynchronizer sync)
		{
			sync.ColumnHeaderHeightChanging += new EventHandler(Columns_ColumnHeaderHeightChanging);
			sync.ColumnHeaderHeightChanged += new EventHandler(Columns_ColumnHeaderHeightChanged);
            sync.ColumnWidthChanging += new DColumnWidthEventHandler(Columns_ColumnWidthChanging);
            sync.ColumnWidthChanged += new DColumnWidthEventHandler(Columns_ColumnWidthChanged);
            sync.ColumnOrderChanged += new DColumnEventHandler(Columns_ColumnOrderChanged);
            sync.ColumnSortChanged += new DColumnEventHandler(Columns_ColumnSortChanged);
            sync.RowFilterChanged += new DColumnRowFilterEventHandler(Columns_RowFilterChanged);
            sync.TimeAxisChange += new TimeAxisChangeHandler(Columns_TimeAxisChange);
        }
		private void _PrepareEvents(DColumns columns)
		{
            columns.ColumnHeaderHeightChanging += new EventHandler(Columns_ColumnHeaderHeightChanging);
            columns.ColumnHeaderHeightChanged += new EventHandler(Columns_ColumnHeaderHeightChanged);
            columns.ColumnWidthChanging += new DColumnWidthEventHandler(Columns_ColumnWidthChanging);
            columns.ColumnWidthChanged += new DColumnWidthEventHandler(Columns_ColumnWidthChanged);
            columns.ColumnOrderChanged += new DColumnEventHandler(Columns_ColumnOrderChanged);
            columns.ColumnSortChanged += new DColumnEventHandler(Columns_ColumnSortChanged);
            columns.TimeAxisChange += new TimeAxisChangeHandler(Columns_TimeAxisChange);
		}
		private void _LogText(object sender, string message, params object[] args)
		{
			string sndr = (sender is Control ? sender.GetType().Name + ": " + ((Control)sender).Name : sender.ToString());
			string text = String.Format(message, args);
			string log = sndr + ", " + text + Environment.NewLine + this.TextBoxInfo.Text;
			this.TextBoxInfo.Text = log;
		}
        void Columns_ColumnHeaderHeightChanging(object sender, EventArgs e)
        {
            _LogText(sender, "ColumnHeaderHeightChanging, Height=", ((Control)sender).Height);
        }
        void Columns_ColumnHeaderHeightChanged(object sender, EventArgs e)
        {
            _LogText(sender, "ColumnHeaderHeightChanged, Height=", ((Control)sender).Height);
        }
        void Columns_ColumnWidthChanging(object sender, DColumnWidthEventArgs args)
        {
            _LogText(sender, "ColumnWidthChanging, Column[{0}].VisibleWidth = {1}", args.Column.Index, args.Column.VisibleWidth);
        }
        void Columns_ColumnWidthChanged(object sender, DColumnWidthEventArgs args)
		{
            _LogText(sender, "ColumnWidthChanged, Column[{0}].VisibleWidth = {1}", args.Column.Index, args.Column.VisibleWidth);
		}
        void Columns_ColumnOrderChanged(object sender, DColumnEventArgs args)
        {
            _LogText(sender, "ColumnOrderChanged, Column[{0}].ColumnOrder = {1}", args.Column.Index, args.Column.ColumnOrder);
        }
        void Columns_ColumnSortChanged(object sender, DColumnEventArgs args)
		{
			_LogText(sender, "ColumnSortChanged, Column[{0}].Sort = {1}", args.Column.Index, args.Column.SortMode);
		}
		void Columns_TimeAxisChange(object sender, TimeAxisChangeEventArgs e)
		{
			_LogText(sender, "TimeAxisChange, Interval = {0}", e.TimeRange.ToString());
		}

        void Columns_RowFilterChanged(object sender, DColumnRowFilterEventArgs args)
        {
            _LogText(sender, "RowFilterChange, Filter = {0}", args.RowFilter.ToString());
        }
    }
}
